<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="queryForm">
				<el-form-item>
					<el-input v-model="queryForm.keyword" placeholder="部门名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleSave">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.list" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="部门名称" width="120" sortable>
			</el-table-column>
      <el-table-column prop="dirPath" label="路径" width="120" sortable>
      </el-table-column>
      <el-table-column prop="manager.username" label="部门主管" width="120" sortable>
      </el-table-column>
      <el-table-column prop="parent.name" label="上级部门" width="120" sortable>
      </el-table-column>
      <el-table-column prop="state" label="部门状态" width="120" sortable>
        <template scope="scope">
          <span v-if="scope.row.state ==1" style="color: #11b95c">开启中</span>
          <span v-else="scope.row.state ==0" style="color: orangered">关闭中</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="200" sortable>
      </el-table-column>
      <el-table-column prop="updateTime" label="修改时间" width="200" sortable>
      </el-table-column>



			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
<!--			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="pageInfo.total" style="float:right;">-->
<!--			</el-pagination>-->
      <el-pagination
          style="float:right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryForm.currentPage"
          :page-sizes="[5, 10, 20, 40]"
          :page-size="queryForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total">
      </el-pagination>
    </el-col>
		<!--保存界面-->
		<el-dialog title="保存"  :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">

				<el-form-item label="部门名称" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="部门主管" prop="managerId">
          <el-input v-model="saveForm.managerId" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级部门" prop="parentId">
          <el-input v-model="saveForm.parentId" auto-complete="off"></el-input>
        </el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="saveForm.state">
						<el-radio class="radio" :label="1">开启</el-radio>
						<el-radio class="radio" :label="0">关闭</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	// import util from '../../common/js/util'
	// //import NProgress from 'nprogress'
	// import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
        queryForm: {
          keyword: '',
          pageSize: 10,
          currentPage: 1
        },
				pageInfo:{
				  list:[],
          total:0
        },
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					id: 0,
					name: '',
					sex: -1,
					age: 0,
					birth: '',
					addr: ''
				},

				saveFormVisible: false,//新增界面是否显示
				saveLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入部门名称', trigger: 'blur' }
					]
				},
				//新增界面数据
				saveForm: {
          id:null,
          name: '',
          state:null,
          managerId:null,
          parentId:null,
				}

			}
		},
		methods: {
		  //当查询时将要查询的页面置为1：
      getQuery(){
        this.queryForm.currentPage=1;
        this.getDepartments();
      },
		  //当每页条数改变时自动改变
      handleSizeChange(val){
        this.queryForm.pageSize=val;
        this.getDepartments();
      },
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.queryForm.currentPage = val;
        this.getDepartments();
			},
			//获取部门列表
			getDepartments() {
        //发送axios请求到后台springboot接口
        this.$http.post("/department",this.queryForm)
        .then(res=>{
          console.debug("成功",res)
          var data = res.data
          if (data.success){
            this.pageInfo=data.data
          }
        }).catch(res=>{
          console.debug("失败",res)
        })

			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
          this.listLoading=true;
          //发送请求
          this.$http.delete("/department/"+row.id)
          .then(res=>{
            var data = res.data
            if (data.success){
              this.listLoading=false;
              this.queryForm.currentPage=1;
              this.getDepartments();
            }
          })
				}).catch(res => {
              console.debug(res,"删除失败")
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.saveFormVisible=true;
				this.saveForm=row;

			},
			//显示新增界面
			handleSave: function () {
				this.saveFormVisible = true;
				this.saveForm = {
				  id:null,
					name: '',
          state:null,
          managerId:null,
          parentId:null,
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							editUser(para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getDepartments();
							});
						});
					}
				});
			},
			//保存
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.saveLoading = true;
							this.$http.put("/department",this.saveForm)
                  .then(res=>{
                    if (res.data.success){
                      this.saveLoading=false;
                      this.saveFormVisible=false;
                      this.handleCurrentChange(1);
                    }
                  })
            });
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
				  this.listLoading=true;
				  this.$http.patch("/department/",ids)
              .then(res =>{
              if (res.data.success){
                this.listLoading=false;
                this.handleCurrentChange(1);
              }
          })
        }).catch(() => {

				});
			}
		},
		mounted() {
			this.getDepartments();
		}
	}

</script>

<style scoped>

</style>